using Microsoft.OpenApi.Models;
using qYun.ApiService;
using qYun.ApiService.Models;
using qYun.ApiService.Services;
using qYun.Shared.Models;

var builder = WebApplication.CreateBuilder(args);

// Add service defaults & Aspire components.
builder.AddServiceDefaults();
builder.Configuration.AddJsonFile("Config/SystemSetting.json",
    optional: false,
    reloadOnChange: true);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    var basePath = AppContext.BaseDirectory;
    var xmlPath = Path.Combine(basePath, "qYun.ApiService.xml");
    options.IncludeXmlComments(xmlPath);
    options.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "Your API Name",
        Version = "v1",
        Description = "A sample API using Swagger in .NET 8"
    });
});
var connectString = builder.Configuration.GetConnectionString("DefaultConnection");
// builder.Services.AddDbContext<QyunContext>(options =>
//     options.UseMySql(connectString,ServerVersion.AutoDetect(connectString)));
builder.Services.AddScoped<IPlcService,PlcService>();
builder.Services.AddScoped<IGasService,GasService>();
builder.Services.AddScoped<IYunheService,YunheService>();
builder.Services.AddScoped<IDbService,DbService>();
builder.Services.AddSingleton<SharedStateService>();
builder.Services.Configure<SystemConfig>(builder.Configuration);

// Add services to the container.
builder.Services.AddProblemDetails();
builder.Services.AddSignalR();
// 注册服务
builder.Services.AddSingleton<IHostedService,CustomInitializer>();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseExceptionHandler();

// var summaries = new[]
// {
//     "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
// };
//
// app.MapGet("/weatherforecast", () =>
// {
//     var forecast = Enumerable.Range(1, 5).Select(index =>
//         new WeatherForecast
//         (
//             DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
//             Random.qYun.Shared.Next(-20, 55),
//             summaries[Random.qYun.Shared.Next(summaries.Length)]
//         ))
//         .ToArray();
//     return forecast;
// });

// 添加 Swagger UI 路由
app.UseSwagger();
app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API v1");
});
app.MapDefaultEndpoints();
app.MapControllers();
app.MapHub<DataHub>("update");

app.Run();

// record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
// {
//     public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
// }
